<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择部门负责人">
          <!-- 将获取数据进行铺设 -->
          <el-option v-for="item in employees " :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="deptSubmit">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getEmployeeSimple,
  addDepartments,
  getDepartDetail,
  updateDepartments } from '@/api/departments'
export default {
  // 方便调试
  // 标识当前组件的别名
  name: 'DeptDialog',
  props: {
    parentId: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      require: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 验证编码是否存在
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      existCodeList.includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
      // return {}
    }
    // 验证名称 部门名称在同级不能重复
    const validName = (rule, value, callback) => {
      let existNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      if (this.isEdit) {
        // 先获取本身
        const obj = this.originList.find(item => item.id === this.parentId)
        // 获取到本身以后 才能够获取pid
        const pid = obj.pid
        // 根据pid 才能够 获取到同级 获取到同级同时需要将本身排除
        existNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId)
        // this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      }
      existNameList.includes(value) ? callback(new Error(`部门名称${value}已经存在`)) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [], // 负责人列表
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    // 调用获取负责人列表的方法
    this.getEmployeeData()
    // 调用获取详情的方法
    this.getDepartDetail()
  },
  methods: {
  // 添加编辑确认按钮
    async addDepts() {
      // 新增组织架构方法
      console.log(123)
      const res = await addDepartments({ ...this.form, pid: this.parentId })
      // 给用户提示
      this.$message.success(res.message)
      // 父级关闭弹窗
      // 父级渲染列表
      this.$emit('update-list')
    },
    // 编辑组织架构的方法
    async editDepts() {
      const res = await updateDepartments(this.form)
      this.$message.success(res.message)
      this.$emit('update-list')
    },
    // 点击确定的方法
    deptSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.editDepts() : this.addDepts()
      })
    },

    // 取消编辑确认按钮
    cancel() {
      this.$emit('close')
    },
    async getEmployeeData() {
      const res = await getEmployeeSimple()
      if (!res.success) return this.$message.error(res.message)
      this.employees = res.data
    },
    // 获取部门详情
    async getDepartDetail() {
      if (this.isEdit) {
        this.form = {}
        const res = await getDepartDetail(this.parentId)
        this.form = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
